package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.MeetingManagement;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.mapper.MeetingManagementMapper;
import com.ruoyi.system.mapper.SysPostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MeetingUserSignatureMapper;
import com.ruoyi.system.domain.MeetingUserSignature;
import com.ruoyi.system.service.IMeetingUserSignatureService;

/**
 * 会议签到Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-02
 */
@Service
public class MeetingUserSignatureServiceImpl implements IMeetingUserSignatureService
{
    @Autowired
    private MeetingUserSignatureMapper meetingUserSignatureMapper;
    @Autowired
    private SysPostMapper sysPostMapper;
    @Autowired
    private MeetingManagementMapper meetingManagementMapper;

    /**
     * 查询会议签到
     *
     * @param id 会议签到主键
     * @return 会议签到
     */
    @Override
    public MeetingUserSignature selectMeetingUserSignatureById(Long id)
    {
        return meetingUserSignatureMapper.selectMeetingUserSignatureById(id);
    }

    /**
     * 查询会议签到列表
     *
     * @param meetingUserSignature 会议签到
     * @return 会议签到
     */
    @Override
    public List<MeetingUserSignature> selectMeetingUserSignatureList(MeetingUserSignature meetingUserSignature)
    {
        List<MeetingUserSignature> meetingUserSignatureList = meetingUserSignatureMapper.selectMeetingUserSignatureList(meetingUserSignature);
        for(MeetingUserSignature signature : meetingUserSignatureList){
            String userPostId = signature.getUserPostId();
            if(StringUtils.isNotBlank(userPostId)){
                List<String> userPostIdList = Arrays.asList(userPostId.split(","));
                List<String> postNameList = new ArrayList();
                for(String postId : userPostIdList){
                    SysPost sysPost = sysPostMapper.selectPostById(Long.valueOf(postId));
                    postNameList.add(sysPost.getPostName());
                }
                signature.setPostNameList(postNameList);
            }
            String status = signature.getStatus();
            //迟到
            if("3".equals(status)){
                //签到时间
                String signatureTime = signature.getSignatureTime();
                Date signatureTimeDate = DateUtil.parse(signatureTime, "yyyy-MM-dd HH:mm:ss");
                MeetingManagement meetingManagement = meetingManagementMapper.selectMeetingManagementById(meetingUserSignature.getId());
                String meetingStartTime = meetingManagement.getMeetingStartTime();
                Date meetingStartTimeDate = DateUtil.parse(meetingStartTime, "yyyy-MM-dd HH:mm:ss");
                //会议开始5分钟之后算时间
                Date startDate = DateUtil.offsetMinute(meetingStartTimeDate, 5);
                //计算迟到时间
                long between = DateUtil.between(signatureTimeDate, startDate, DateUnit.MINUTE);
                signature.setLayTime((int) between);
            }
        }
        return meetingUserSignatureList;
    }

    /**
     * 新增会议签到
     *
     * @param meetingUserSignature 会议签到
     * @return 结果
     */
    @Override
    public int insertMeetingUserSignature(MeetingUserSignature meetingUserSignature)
    {
        meetingUserSignature.setCreateTime(DateUtils.getNowDate());
        List<String> postIdList = meetingUserSignature.getPostIdList();
        String postId = StringUtils.join(postIdList, ",");
        meetingUserSignature.setUserPostId(postId);
        return meetingUserSignatureMapper.insertMeetingUserSignature(meetingUserSignature);
    }

    /**
     * 修改会议签到
     *
     * @param meetingUserSignature 会议签到
     * @return 结果
     */
    @Override
    public AjaxResult updateMeetingUserSignature(MeetingUserSignature meetingUserSignature)
    {
        meetingUserSignature.setUpdateTime(DateUtils.getNowDate());
        MeetingUserSignature meetingUserSignatureVo = meetingUserSignatureMapper.selectMeetingUserSignatureById(meetingUserSignature.getId());
        MeetingManagement meetingManagement = meetingManagementMapper.selectMeetingManagementById(meetingUserSignatureVo.getMeetingId());
        String meetingStartTime = meetingManagement.getMeetingStartTime();
        Date meetingStartTimeDate = DateUtil.parse(meetingStartTime, "yyyy-MM-dd HH:mm:ss");
        //会议开始前15分钟签到开始
        Date startDate = DateUtil.offsetMinute(meetingStartTimeDate, -15);
        //会议结束 签到结束
        String meetingEndTime = meetingManagement.getMeetingEndTime();
        Date meetingEndTimeDate = DateUtil.parse(meetingEndTime, "yyyy-MM-dd HH:mm:ss");
        Date nowDate = DateUtils.getNowDate();
        boolean in = DateUtil.isIn(nowDate, startDate, meetingEndTimeDate);
        if(!in){
            return AjaxResult.error("不在签到时间范围");
        }
        //签到时间
        meetingUserSignature.setSignatureTime(DateUtil.format(nowDate,"yyyy-MM-dd HH:mm:ss"));
        //会议开始5分钟之后算迟到
        Date layDate = DateUtil.offsetMinute(meetingStartTimeDate, 5);
        int compare = DateUtil.compare(layDate, nowDate);
        if(compare>0){
            //正常签到
            meetingUserSignature.setStatus("1");
        }else{
            //迟到
            meetingUserSignature.setStatus("3");
        }

        meetingUserSignatureMapper.updateMeetingUserSignature(meetingUserSignature);
        return AjaxResult.success("签到成功");
    }

    /**
     * 删除会议签到信息
     *
     * @param id 会议签到主键
     * @return 结果
     */
    @Override
    public int deleteMeetingUserSignatureById(Long id)
    {
        return meetingUserSignatureMapper.deleteMeetingUserSignatureById(id);
    }

    /**
     * 根据会议类型查询人员签到次数
     *
     * @param meetingUserSignature 会议签到
     * @return 会议签到集合
     */
    @Override
    public List<MeetingUserSignature>  selectUserSignatureCountByMeetingType(MeetingUserSignature meetingUserSignature){
        return meetingUserSignatureMapper.selectUserSignatureCountByMeetingType(meetingUserSignature);
    }
}
